

#import "ContactListTableViewCell.h"

@interface ContactListTableViewCell()
- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold;
@end

@implementation ContactListTableViewCell

@synthesize personNameLabel = _personNameLabel;
@synthesize personPhoneLabel = _personPhoneLabel;
@synthesize personCompanyAndJobTitleLabel = _personCompanyAndJobTitleLabel;


+ (void)initialize
{
	//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;	
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView *myContentView = self.contentView;
        
		
		self.personNameLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:17.0 bold:YES]; 
		self.personNameLabel.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.personNameLabel];
		[self.personNameLabel release];
        
        self.personPhoneLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:YES]; 
		self.personPhoneLabel.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.personPhoneLabel];
		[self.personPhoneLabel release];

        self.personCompanyAndJobTitleLabel = [self newLabelWithPrimaryColor:[UIColor darkGrayColor] selectedColor:[UIColor whiteColor] fontSize:12.0 bold:NO];
		self.personCompanyAndJobTitleLabel.textAlignment = UITextAlignmentLeft;
		[myContentView addSubview:self.personCompanyAndJobTitleLabel];
		[self.personCompanyAndJobTitleLabel release];
        
    }
    return self;
}

- (Person *)person
{
    return _person;
}


- (void)setPerson:(Person *)newPerson
{
    [newPerson retain];
    [_person release];
    _person = newPerson;	
    self.personNameLabel.text = [NSString stringWithFormat:@"%@ %@", newPerson.firstName, newPerson.lastName];
	self.personPhoneLabel.text= newPerson.workPhone;
	NSMutableString* job=[[NSMutableString alloc] init];
	if(newPerson.jobTitle!=nil){
		[job appendString:newPerson.jobTitle];
	}
	if(newPerson.jobTitle!=nil && newPerson.company!=nil)
		[job appendString:@" of "];
	if(newPerson.company!=nil){
		[job appendString:newPerson.company];
	}
    self.personCompanyAndJobTitleLabel.text = job;
	[job release];
	[self setNeedsDisplay];
}




- (void)layoutSubviews {
    

#define UPPER_ROW_OFFSET 6
#define LOWER_ROW_OFFSET 24
#define GAP 10
#define NAME_WIDTH 190
#define ROW_HEIGHT 18		
#define	PHONE_WIDTH 100
#define COLUMN_OFFSET 5
#define COMPANY_WIDTH 270

    
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
	
	if (!self.editing) {
		
        CGFloat boundsX = contentRect.origin.x + 12;
		CGRect frame;
		//THE NAME
		frame = CGRectMake(boundsX , UPPER_ROW_OFFSET, NAME_WIDTH, ROW_HEIGHT);
		self.personNameLabel.frame = frame;
		
		//THE PHONE
		frame = CGRectMake(boundsX + GAP+NAME_WIDTH, UPPER_ROW_OFFSET, PHONE_WIDTH, ROW_HEIGHT);
		self.personPhoneLabel.frame = frame;
	
        
        // THE Job and company
        frame = CGRectMake(boundsX, LOWER_ROW_OFFSET, COMPANY_WIDTH, ROW_HEIGHT);
        self.personCompanyAndJobTitleLabel.frame = frame;
		
	}
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	/*
	 Views are drawn most efficiently when they are opaque and do not have a clear background, so in newLabelForMainText: the labels are made opaque and given a white background.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  
     */
	[super setSelected:selected animated:animated];
	
	UIColor *backgroundColor = nil;
	if (selected) {
	    backgroundColor = [UIColor clearColor];
	} else {
		backgroundColor = [UIColor whiteColor];
	}
    
	
	self.personNameLabel.backgroundColor = backgroundColor;
	self.personNameLabel.highlighted = selected;
	self.personNameLabel.opaque = !selected;
	
		
	self.personPhoneLabel.backgroundColor = backgroundColor;
	self.personPhoneLabel.highlighted = selected;
	self.personPhoneLabel.opaque = !selected;
	
	self.personCompanyAndJobTitleLabel.backgroundColor = backgroundColor;
	self.personCompanyAndJobTitleLabel.highlighted = selected;
	self.personCompanyAndJobTitleLabel.opaque = !selected;
}

- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold
{
	/*
        Create and configure a label.
    */

    UIFont *font;
    if (bold) {
        font = [UIFont boldSystemFontOfSize:fontSize];
    } else {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    /*
        Views are drawn most efficiently when they are opaque and do not have a clear background, so set these defaults.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  This is handled in setSelected:animated:.
    */
	UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	newLabel.backgroundColor = [UIColor whiteColor];
	newLabel.opaque = YES;
	newLabel.textColor = primaryColor;
	newLabel.highlightedTextColor = selectedColor;
	newLabel.font = font;
	
	return newLabel;
}




@end
